//
//  DoorLockSettingViewController.m
//  RONGAO
//
//  Created by Kendama on 2018/2/26.
//  Copyright © 2018年 wei wang. All rights reserved.
//

#import "DoorLockSettingViewController.h"
#import "UnlockingModelViewController.h"
#import "SafetyManagementViewController.h"
#import "DoorLockInfoViewController.h"
#import "AdminPasswordViewController.h"
#import "DoorPushApi.h"
#import "DoorGetPushInfoApi.h"

@interface DoorLockSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

/** UItableView **/
@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) NSArray *dataSoure;
@property(nonatomic,strong) UISwitch *switchButton;

@end

@implementation DoorLockSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getPushStates];
}

- (void)getPushStates {
    __weak typeof(self) weakself = self;
    DoorGetPushInfoApi *api = [[DoorGetPushInfoApi alloc]init];
    
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = request.responseJSONObject;
        if ([dic[@"errcode"] intValue] == 0) {
            NSString *ispush = dic[@"data"][@"is_push"];
            [weakself.switchButton setOn:([ispush isEqualToString:@"1"] ? YES : NO)];
        } else {
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:request.responseJSONObject[@"errmsg"]];
    }];
}

#pragma mark - --------------------------点击事件--------------------------
- (void)switchAction:(UISwitch *)sender
{
    NSString *isPush = @"0";
    if (sender.on == YES)
    {
        isPush = @"1";
    }
    DoorPushApi *api = [[DoorPushApi alloc]initWithDic:@{@"is_push":isPush}];
    
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = request.responseJSONObject;
        NSLog(@"%@",dic);
        if ([dic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showSuccessWithStatus:dic[@"errmsg"]];
        } else {
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
        
    } failure:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:request.responseJSONObject[@"errmsg"]];
    }];
}

#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSoure.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

#pragma mark - --------------------------UITableView delegate--------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = self.dataSoure[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    if (indexPath.row == 0)
    {
        __weak UIView *superView = cell.contentView;
        [superView addSubview:self.switchButton];
        [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(superView.mas_centerY);
            make.right.mas_equalTo(superView.mas_right).with.offset(-20);
        }];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            //开锁模式
            UnlockingModelViewController *vc = [UnlockingModelViewController new];
            vc.deviceMac = self.deviceMac;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            //安全管理
            SafetyManagementViewController *vc = [SafetyManagementViewController new];
            vc.deviceMac = _deviceMac;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            DoorLockInfoViewController *vc = [DoorLockInfoViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            AdminPasswordViewController *vc = [AdminPasswordViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}




- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}


#pragma mark - --------------------------懒加载--------------------------
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (NSArray *)dataSoure{
    
    if (!_dataSoure) {
        _dataSoure = @[@"通知消息",@"开锁模式",@"安全管理"];
    }
    return _dataSoure;
}

- (UISwitch *)switchButton
{
    if(!_switchButton)
    {
        _switchButton = [[UISwitch alloc]init];
//        _switchButton.tintColor = [UIColor colorWithRed:217/255.0 green:182/255.0 blue:107/255.0 alpha:1];
        _switchButton.onTintColor = [UIColor colorWithRed:217/255.0 green:182/255.0 blue:107/255.0 alpha:1];
        [_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}


#pragma mark - --------------------------约束--------------------------
- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
}

#pragma mark - --------------------------导航栏--------------------------
- (void)setNavigationStyle
{
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setNavigationTitle:@"门锁设置" titleColor:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
